#include "../unp.h"
#include <time.h>

/***
 * 
 */

int
main(int argc, char **argv)
{
    int listenfd, connfd;
    struct sockaddr_in servaddr;
    char buff[MAXLINE];
    time_t ticks;

    listenfd = Socket(AF_INET, SOCK_STREAM, 0);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(13);



    /**
     * Bind函数第二参数
     * 
     * 
     * 定义为 
struct sockaddr {
	sa_family_t sa_family;
	char sa_data[14];
};
        实际上用sockaddr_in强转

struct sockaddr_in {
	sa_family_t sin_family;
	in_port_t sin_port;        16位  2字节
	struct in_addr sin_addr;   32位 4字节   
	uint8_t sin_zero[8];
};

    2+4+8 等于 char sa_data[14]
     */
    Bind(listenfd,(SA*) &servaddr, sizeof(servaddr));

    Listen(listenfd, LISTENQ);

    for(;;)
    {
        connfd = Accept(listenfd,(SA*)NULL, NULL);
        ticks = time(NULL);
        snprintf(buff, sizeof(buff),"%.24s\r\n",ctime(&ticks));
        Write(connfd, buff, strlen(buff));
        Close(connfd);
    }

    return 0;
}